# ======================================================================
#
# Copyright (C) ADN
# ADN::SERVICE::Service Service.pm
# Description:
#  - Gestion des services de la sonde
#
# ======================================================================

package ADN::SERVICE::Service;

#use strict;
use vars qw($AUTOLOAD);
use ADN::XML::XMLtoOBJ;
use ADN::XML::OBJtoXML;
use ADN::CONF::Read;
use File::Path;

# #######################
# Création de l'objet self + init. variables
# #######################
sub new
{
	# Construction de l'objet $self
	my $self = shift;
	my $class = ref($self) || $self;
	return $self if ref $self;
	$self = bless {} => $class;
	$self->{error} = 0;
  $self->_init();
	return $self;
}

# #######################
# Fonction initialisation
# #######################
sub _init
{
	my $self = shift;
  # Chargement du fichier de configuration XML + Initialisation variable.
  my $conf = ADN::CONF::Read->new();
  ($conf =~ m/^[-]1$/smx) ? ($self->{error} = 1) : ();
  if ($self->{error} != 1)
  {
    my %service_server = $conf->service_server();
    ($conf->service_server() =~ m/^[-]1$/smx) ? ($self->{error} = 1) : ();
	  $self->{service_path} = $service_server{service_path};
	  $self->{service_file} = $service_server{service_file};
	  $self->{path_inc} = $conf->_send_path();
	  undef $conf;
  }
  return;
}

# #######################
# Fonction lecture de fichier
# #######################
sub file_xml
{
	my $self = shift;
	my ($balise) = @_;
  my $file = $self->{path_inc}.$self->{service_path}.$self->{service_file};

  if (-e $file && -f $file && -r $file)
  {
	  if(open FILE, '<', $file)
	  {
		  while ( my $l1 = <FILE>)
		  {
			  $self->{xml} .= $l1;
		  }
		  if (!close FILE) {}
	  }
	}
	undef $file;
  return $self->division_xml($balise);
}

# #######################
# Fonction Mise en place XML
# #######################
sub division_xml
{
	my $self = shift;
	my ($balise) = @_;

	if ($self->{xml} =~ m/\<$balise\>/smx)
	{
  	$self->{xml} =~ s/(\<[^\>]+\>)[^\<&^\>]*(\<\/[^\>]+\>)/$1$2/gsmx;
    $self->{xml} =~ s/\s|\n|\r//gsmx;
  	$self->{xml} =~ s/^\<[^\>]+\>(.*)\<[^\>]+\>$/$1/smx;
    $self->{xml} =~ s/^.*\<$balise\>(.*)\<\/$balise\>.*$/$1/smx;
  	$self->{xml} =~ s/\<\/[^\>]+\>//gsmx;
  	$self->{xml} =~ s/^\<|\>$//gsmx;
  	@{$self->{xml1}} = split/\>\</smx, $self->{xml};
 		undef $balise;
    return @{$self->{xml1}};
  }
  else
  { 
    $self->{error} = 1;
  }
 	undef $balise;
  return;
}


# #######################
# Fonction DESTROY
# #######################
sub DESTROY
{
  my $self = shift;
  undef $self;
  return;
}

# #######################
# Fonction erreur XML ou Balise
# #######################
sub gestion_error
{
  my $self = shift;
  my %hach;
  $hach{type} = 0;

  if ($self->{error} != 0)
  {
    if ($self->{error} == 1)
    { 
      $hach{type} = -1;
    }
    elsif ($self->{error} == 2)
    {
      $hach{type} = -2;
    }
    elsif ($self->{error} == 3)
    {
      $hach{type} = -3;
    }
  }
  return %hach;
}

# #######################
# Fonction Control des parametres
# #######################
sub ctr_param
{
  my $self = shift;
  my ($p, $nbr) = @_;
  my $ctr;
  my $verif;

  if (($p =~ m/^(param)$/smx) and ($nbr =~ m/^[0-9]+$/smx))
  {
    while ($nbr != 0)
    {
      if ($nbr != 0)
      {
        $verif = "p".$nbr;
        $ctr = 0;
        foreach my $l2 (@{$self->{xml1}})
        {
          if ($l2 =~ m/^$verif$/smx)
          {
             $ctr = 1;
          }
        }
        if ($ctr != 1)
        {
          $self->{error} = 3;
        }
      }
      $nbr--;
    }
  }
  undef $ctr;
  undef $verif;
  undef $p;
  undef $nbr;
  return;
}

# #######################
# Fonction AUTOLOAD
# #######################
sub AUTOLOAD
{
  my $self = shift;
  my $balise = $AUTOLOAD;
  $balise =~ s/.*:://smx;
  my ($balise2) = @_;
  my %hach;
  my $cnt = 1;
  my (@temp, @result);

  if (defined $balise and defined $balise2)
  {
    if ($balise =~ m/^[a-zA-Z0-9\_]+$/smx)
    {
      if (defined $self->{path_inc} and defined $self->{service_path} and defined $self->{service_file})
      {
        my $xml =  ADN::XML::XMLtoOBJ->new($self->{path_inc}.$self->{service_path}.$self->{service_file});
        ($xml =~ m/^[-]1$/smx) ? ($self->{error} = 1) : (); # Controle de sortie du module XMLtoOBJ
        (($xml->$balise()) =~ m/^[-]1$/smx) ? ($self->{error} = 1) : ();
        undef $balise;
				(($xml->$balise2()) =~ m/^[-]1$/smx) ? ($self->{error} = 1) : (@result = $xml->$balise2());
        $self->file_xml($balise2);
        undef $balise2;
        undef $xml;
				($self->{error} != 0) ? (return $self->gestion_error()) : ();
        foreach my $l (@{$self->{xml1}})
        {
            @temp = $result[0]->$l();
            $self->ctr_param($l, $temp[0]);
            ($temp[0] =~ m/^$/smx) ? ($self->{error} = 2) : ();
            ($self->{error} != 0) ? (return $self->gestion_error()) : ();
            $hach{$l} = $temp[0];
        }
        undef @result;
        undef @temp;
        return %hach;
      }
      else
      { 
        $self->{error} = 1;
        undef @result;
        undef @temp;
        undef %hach;
        return $self->gestion_error();
      }
    }
  }
  return;
}
1;
__END__
